<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>批量设置加人规则</title>
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../assets/style/admin.css">
    <style>
        .layui-form-label {
            min-width: 180px;
        }
    </style>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <script src="../assets/layui/layui.js"></script>
    <script src="../assets/js/common.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script src="../common/header.js"></script>
    <!-- 内容主体区域 -->
    <div id="LAY_app_body">
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">
                            批量设置加人规则
                        </div>
                        <div class="layui-card-body">
                            <form class="layui-form" action="" onsubmit="return false;">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">每天加人轮数</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="add_friend_day_limit" lay-verify="add_friend_day_limit"
                                               autocomplete="off" placeholder="每天加人轮数" class="layui-input" value="">
                                    </div>
                                    <label class="layui-form-mid layui-word-aux">设置为0则不加好友。</label>
                                </div>
                                <div class="layui-form-item layui-hide">
                                    <label class="layui-form-label">手机号每轮加人次数</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="add_friend_per_round" lay-verify="add_friend_per_round"
                                               autocomplete="off" placeholder="手机号每轮加人次数"
                                               class="layui-input layui-disabled" value="1" readonly>
                                    </div>
                                    <label class="layui-form-mid layui-word-aux">设置为0则不加好友</label>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">手机号加人每轮间隔</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="upload_friend_interval"
                                               lay-verify="upload_friend_interval" autocomplete="off"
                                               placeholder="手机号加好友间隔" class="layui-input" value="">
                                    </div>
                                    <label class="layui-form-mid layui-word-aux">单位：分钟，不能低于20分钟</label>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">微信号和QQ号加好友间隔</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="add_friend_interval" lay-verify="add_friend_interval"
                                               autocomplete="off" placeholder="微信号和QQ号加好友间隔" class="layui-input"
                                               value="60">
                                    </div>
                                    <label class="layui-form-mid layui-word-aux">单位：分钟，不能低于60分钟</label>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">选择微信组</label>
                                    <label class="layui-form-mid layui-word-aux">请先给微信设置分组，然后在此处选择</label>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                        <a class="layui-btn" lay-submit lay-filter="formSubmit">确认设置</a>
                                        <a class="layui-btn layui-btn-primary" href="../../code/wechat/index.html">取消</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        var selectGroup;
        var removeGroup;
        var getExistGroup;
        var checkExistGroup;
        layui.use(['form'], function () {
            var $ = layui.$
                , form = layui.form;

            form.verify({
                add_friend_day_limit: function (value) {
                    if (value < 0 || value > 100) {
                        return '请输入每天加人轮数合理上限';
                    }
                }
                , add_friend_interval: function (value) {
                    if (value < 60) {
                        return '搜索加人间隔时间不能小于60分钟';
                    }
                }
                , upload_friend_interval: function (value) {
                    if (value < 20) {
                        return '手机号每轮加人间隔时间不能小于20分钟';
                    }
                }
                , add_friend_per_round: function (value) {
                    if (value > 1) {
                        return '手机号每轮加人不能超过1个';
                    }
                }
            });

            //监听提交
            form.on('submit(formSubmit)', function (data) {
                var hasGroup = getExistGroup();
                if (!hasGroup) {
                    layer.msg('请选择执行的微信组', {icon: 5});
                    return false
                }
                layer.msg('提交中', {
                    shadeClose: false
                    , icon: 16
                    , shade: 0.01
                });
                $.ajax({
                    type: "POST",
                    url: "/wechat_rule/batchSave",
                    data: data.field,
                    success: function (response) {
                        var dataObj = $.parseJSON(response);
                        if (dataObj.code === 0) {
                            layer.msg('设置成功', {icon: 6});
                            setTimeout(function () {
                                location.href = '/wechat/index';
                            }, 1000);
                        } else {
                            layer.msg(dataObj.msg, {icon: 5});
                        }
                    },
                    error: function (request, status, error) {
                        layer.msg('哦噢，网络开小差了', {icon: 5});
                    }
                });
                return false;
            });

            selectGroup = function (groupId) {
                var group = $('#availableGroup' + groupId);
                var newGroupName = filterHTMLTag(group.html());
                var html = '<button id="selectedGroup' + groupId + '" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-primary" onclick="removeGroup(' + groupId + ');">' + newGroupName + '&nbsp;<i class="layui-icon layui-icon-close-fill" style="color: orange;"></i><input type="hidden" name="group_id[]" class="existGroup" value="' + groupId + '"></button>';
                $('#exist_group').append(html);
                group.remove();
                checkExistGroup();
            };

            removeGroup = function (groupId) {
                if (groupId > 0) {
                    var group = $('#selectedGroup' + groupId);
                    var newGroupName = filterHTMLTag(group.html());
                    var html = '<button class="layui-btn layui-btn-sm layui-btn-radius layui-btn-primary" onclick="selectGroup(' + groupId + ');" id="availableGroup' + groupId + '">' + newGroupName + '&nbsp;<i class="layui-icon layui-icon-add-circle" style="color: green;"></i></button>';
                    $('#availableGroups').prepend(html);
                    group.remove();
                }
                checkExistGroup();
            };

            getExistGroup = function () {
                var objs = $('.existGroup');
                var hasGroup = false;
                $.each(objs, function (index, obj) {
                    hasGroup = true;
                });
                return hasGroup;
            };

            checkExistGroup = function () {
                var hasGroup = getExistGroup();
                if (hasGroup === true) {
                    $('#no_group').css('display', 'none');
                } else {
                    $('#no_group').css('display', 'block');
                }
            };
        });

        var filterHTMLTag = function (msg) {
            msg = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag
            msg = msg.replace(/[|]*\n/, ''); //去除行尾空格
            msg = msg.replace(/&nbsp;/ig, ''); //去掉nbsp
            return msg;
        };
    </script>
</div>
</body>
</html>
